<template>
    <rx-dialog @ok="handleSubmit" @cancel="cancel">
        <a-form ref="form" layout="vertical" :model="mdl" :rules="rules" class="rx-a-form-vertical">
            <a-form-item style="display:none;">
                <a-input v-model:value="mdl.deliverUserId"/>
                <a-input v-model:value="mdl.deliverUserNo"/>
                <a-input v-model:value="mdl.receiptUserId"/>
                <a-input v-model:value="mdl.receiptUserNo"/>
            </a-form-item>
            <a-form-item name="deliverUserName" :label="i18n('form.transferee','移交人')">
                <a-input-group compact v-model:value="mdl.deliverUserName">
                    <rx-text-list
                        v-model="deliverUser" valuefield="id"
                        textfield="name" @click="joinUser(1)"
                        :showclose="false"
                        style="display: flex"
                    >
                        <my-icon type="iconuser-account-add"></my-icon>
                    </rx-text-list>
                </a-input-group>
            </a-form-item>
            <a-form-item name="receiptUserName" :label="i18n('form.receiver','接管人')">
                <a-input-group compact v-model:value="mdl.receiptUserName">
                    <rx-text-list v-model="receiptUser" valuefield="id" textfield="name" @click="joinUser(2)"
                                  :showclose="false"
                                  style="display: flex"
                    >
                        <my-icon type="iconuser-account-add"></my-icon>
                    </rx-text-list>
                </a-input-group>
            </a-form-item>
            <a-form-item name="comment" :label="$ti18('rxForm.notes','备注')">
                <a-input-group compact style="display: flex;margin-top: 6px;">
                    <a-textarea v-model:value="mdl.comment" style="flex: 1;margin-right: 0;"/>
                </a-input-group>
            </a-form-item>
        </a-form>
    </rx-dialog>
</template>
<script>
import BpmDeliverApi from '@/common/api/bpm/bpmDeliver'
import {BaseFormModel, RxDialog, RxTextList} from "@lowcode/jpaas-base-lib"
import {ShareDialog} from '@lowcode/jpaas-share-component'

export default {
    name: 'BpmDeliverEdit',
    mixins: [BaseFormModel],
    components: {
        RxDialog,
        RxTextList
    },
    data() {
        return {
            rules: {
                deliverUserName: [{required: true, message: this.i18n('form.transferee','请选择移交人','select'), trigger: 'change'}],
                receiptUserName: [{required: true, message: this.i18n('form.receiver',"请选择接管人",'select'), trigger: 'change'}],
            },
            deliverUser: [],
            receiptUser: [],
        }
    },
    methods: {
        i18n(name, text, key) {
            return this.$ti18(name, text, "bpmDeliver", key);
        },
        get(id) {
            return BpmDeliverApi.get(id);
        },
        validForm(values) {
            if (values.deliverUserId == values.receiptUserId) {
                this.$message.error(this.i18n('notSame', '移交人和接管人不能选择同一个人！'));
                return false;
            }

            return true;
        },

        save(values) {
            return BpmDeliverApi.save(values);
        },

        joinUser(type) {
            let conf = {
                curVm: this, data: {single: true}, widthHeight: ['900px', '700px']
            };
            //type 1: 交接人 2, 接收人
            let valid = type != 1

            conf.data.onlyValid = valid;
            let self = this;

            ShareDialog.openUserDialog(conf, function (user) {
                if (user == null) {
                    self.$message.error(this.i18n('noSelect', '未选择用户！'));
                    return;
                }

                if (type == 1) {
                    self.deliverUser = [
                        {
                            name: user.fullName,
                            id: user.userId
                        }
                    ]
                    Object.assign(self.mdl, {
                        deliverUserId: user.userId,
                        deliverUserNo: user.userNo,
                        deliverUserName: user.fullName
                    });
                } else {
                    self.receiptUser = [
                        {
                            name: user.fullName,
                            id: user.userId
                        }
                    ]
                    Object.assign(self.mdl, {
                        receiptUserId: user.userId,
                        receiptUserNo: user.userNo,
                        receiptUserName: user.fullName
                    });
                }
            })
        },

    }
}
</script>
<style>
.relick a {
    margin-right: 10px;
}

</style>
